<!--- Document Information -----------------------------------------------------

Title:      HibernateFactory.cfc

Author:     Mark Mandel
Email:      mark@compoundtheory.com

Website:    http://www.compoundtheory.com

Purpose:    Factory for getting to Hibernate objects

Usage:      

Modification Log:

Name			Date			Description
================================================================================
Mark Mandel		27/06/2006		Created

------------------------------------------------------------------------------->

<cfcomponent name="HibernateFactory" hint="Factory for getting to Hibernate objects">

<cfscript>
	instance = StructNew();
</cfscript>

<cffunction name="getHibernateFactory" access="private" returntype="any" output="false">
	<cfreturn variables.HibernateFactory />
</cffunction>

<cffunction name="setHibernateFactory" access="private" returntype="void" output="false">
	<cfargument name="HibernateFactory" type="any" required="true">
	<cfset variables.HibernateFactory = arguments.HibernateFactory />
</cffunction>

<cffunction name="getHibernateTools" access="private" returntype="any" output="false">
	<cfreturn variables.HibernateTools />
</cffunction>

<cffunction name="setHibernateTools" access="public" returntype="void" output="false">
	<cfargument name="HibernateTools" type="any" required="true">
	<cfset variables.HibernateTools = arguments.HibernateTools />
</cffunction>

<cffunction name="dumpvar">
  <cfargument name="var" default="blank">
  <cfargument name="abort" default="true">
  <cftry>
	  <cfdump var="#var#">
	<cfcatch>
	  <cfset writeoutput(cfcatch.Message & " " & cfcatch.Detail & " " & cfcatch.TagContext[1].line & " " & cfcatch.stacktrace) />
	</cfcatch>
	</cftry>
	<cfif arguments.abort>
	  <cfabort />
	</cfif>
</cffunction>

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="init" hint="Constructor: Loads the hibernate.cfg.xml from here, and any *.hbm.xml files that are in this directory" access="public" returntype="HibernateService" output="false">
	<cfargument name="hibernateFactory" hint="hibernate factory" required="Yes">
	<cfargument name="configXmlFile" hint="Directory that the hibernate.cfg.xml is in" type="string" required="Yes">
	<cfargument name="hbmXmlDir" hint="Directory that the hibernate.cfg.xml is in" type="string" required="Yes">
	<cfargument name="javafilesdir" hint="Directory that the hibernate.cfg.xml is in" type="string" required="Yes">
	<cfargument name="classfilesdir" hint="Directory that the hibernate.cfg.xml is in" type="string" required="Yes">
	<cfargument name="reverseEngDir" type="string" required="true">
	<cfargument name="hbmExportDir" type="string" required="true">
	<cfscript>
		setHibernateFactory(arguments.hibernateFactory);
		setConfigPath(arguments.configXmlFile,arguments.hbmXmlDir,arguments.javafilesdir,arguments.classfilesdir,arguments.reverseEngDir,arguments.hbmExportDir);		
		return this;
	</cfscript>
</cffunction>

<cffunction name="createHsqldbSchema">
	<cfscript>	
		var myjar = getHibernateFactory().createHsqldbSchema();
	</cfscript>
</cffunction>


<cffunction name="createConfiguration" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="config" required="true" type="struct">
	<cfargument name="hbmsLocation" default="">
	<cfscript>
		var hibConfig = "";
		try {
			if(isStruct(arguments.config)) {
				hibConfig = getHibernateFactory().getConfiguration().configureByStruct(arguments.config);
			} else {
				hibConfig = getHibernateFactory().getConfiguration().configure(expandpath(arguments.config));
			}
			if (arguments.hbmsLocation gt "") {
				hibConfig.addHbmLocation(arguments.hbmsLocation);
			}
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		return hibConfig;
	</cfscript>
</cffunction>

<cffunction name="generate" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
<!---
	<cfargument name="hibernateConfigFile" required="true" />
	<cfargument name="reverseEngConfDirectory" required="true" />
	<cfargument name="hbmsLocation" required="true" />
	<cfargument name="sourcesDirectory" required="true" />
	<cfargument name="classesDirectory" required="true" />
	<cfargument name="jarFileNamePath" required="true" />
--->
	<cfset var jarLoc = "/www/cfmappings/actionpacks/cfhibernate/tests/config/cfhibernate.jar" />
	<cfset var gen = getHibernateTools().generate(variables.configXmlFile,variables.reverseEngDir,variables.hbmExportDir,variables.javafilesdir,variables.classfilesdir,jarLoc) />
	<cfreturn "generated" />
</cffunction>

<cffunction name="createHbmJar" hint="Runs the Hibernate configure method" access="public" returntype="any" output="false">
	<cfargument name="jarFileNamePath" required="true" />
	<cfset var jarLoc = "/www/cfmappings/actionpacks/cfhibernate/tests/config/cfhibernate.jar" />
	<cfset var gen = getHibernateTools().generate(variables.configXmlFile,variables.reverseEngDir,variables.hbmExportDir,variables.javafilesdir,variables.classfilesdir,jarLoc) />
	<cfreturn "generated" />
</cffunction>

	<cffunction name="javagen">
		<cfargument name="hibernateConfigFile" required="true">
		<cfargument name="hbmsLocation" required="true">
		<cfargument name="sourcesDirectory" required="true">
		<cfscript>
			return getHibernateTools().hbm2java(arguments.hibernateConfigFile,arguments.hbmsLocation,arguments.sourcesDirectory);
		</cfscript>	
	</cffunction>

	<cffunction name="compile">
		<cfargument name="srcdir">
		<cfargument name="classpath" default="">
		<cfargument name="outFileDir" default="#arguments.srcdir#">
		<cfscript>
			return getHibernateTools().compile(arguments.srcdir,arguments.classpath,arguments.outFileDir);
		</cfscript>
	</cffunction>


	<cffunction name="hbmgen">
		<cfargument name="configFile" hint="hibernate configuration file" />
		<cfargument name="reverseEngDir" hint="hibernate reverse engineering file" />
		<cfargument name="hbmExportDir" hint="target directory for hbm files" />
		<!--- build test to see if Hibernate launched within CF can
				build a Java class file from a Hibernate mapping file --->
		<cfscript>
		try {
			return getHibernateTools().jdbc2hbm(arguments.configFile,arguments.reverseEngDir,arguments.hbmExportDir);
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		</cfscript>
	</cffunction>

	<cffunction name="createJarFile">
		<cfargument name="srcPath" />
		<cfargument name="destFile" default="#arguments.srcPath#/wee.jar" />
		<cfargument name="jarPrefix" default="" />
		<cfscript>
		var jarUtils = "";
		try {
			jarUtils = getHibernateTools().createJarFile(srcPath,destFile,jarPrefix);
		} 
		catch(Any excpt) {
			// dump of struct is endless loop do to cf introspection haywireness so just do msg/stack
		  dumpvar(excpt.message & " " & excpt.stacktrace);
		}
		</cfscript>
	</cffunction>

<cffunction name="create" hint="Creates a hibernate object" access="public" returntype="any" output="false">
	<cfargument name="className" hint="The class name of the hiberanate class to load" type="string" required="Yes">
	<cfscript>
		return getHibernateFactory().create(arguments.classname);
	</cfscript>
</cffunction>

<cffunction name="hbo" hint="Creates a hibernate object" access="public" returntype="any" output="false">
	<cfargument name="className" hint="The class name of the hiberanate class to load" type="string" required="Yes">
	<cfscript>
		var hbo = createObject("component","HibernateObject");
		hbo.init(getHibernateFactory().create(arguments.classname).newInstance());
		return hbo;
	</cfscript>
</cffunction>

<cffunction name="sessionProxy" hint="Creates a hibernate object" access="public" returntype="any" output="false">
	<cfargument name="session" hint="The class name of the hiberanate class to load" type="string" required="Yes">
	<cfscript>
		var ses = createObject("component","HibernateSession");
		ses.init(arguments.session);
		return hbo;
	</cfscript>
</cffunction>

<cffunction name="getHibernateVersion" hint="The hibernate version in use" access="public" returntype="string" output="false">
	<cfreturn getHibernateFactory().getHibernateVersion() />
</cffunction>

<!------------------------------------------- PACKAGE ------------------------------------------->

<!------------------------------------------- PRIVATE ------------------------------------------->

<cffunction name="dumpPackages" hint="Inititalises all the java libs to be loaded in" access="public" returntype="void" output="false">
	<cfscript>
		var package = createObject("java","java.lang.Package");
		var packages = package.getPackages();
		var allpacks = arrayNew(1);
		var pack = "";
		var packinfo = "";
		for (x = 1; x lte arrayLen(packages); x = x+1) {
			pack = packages[x];
			packinfo = structNew();
			packinfo["name"] = pack.getName();
			if(len(pack.getImplementationVersion())) {
				packinfo["version"] = pack.getImplementationVersion();
			}
			if(len(pack.getSpecificationVersion())) {
				packinfo["specver"] = pack.getSpecificationVersion();
			}
			if(len(pack.getSpecificationTitle())) {
				packinfo["title"] = pack.getSpecificationTitle();
			}
				
			allpacks[x] = packinfo;
		}
		dumpvar(allpacks);
	</cfscript>
</cffunction>


<cffunction name="getConfigPath" access="private" returntype="string" output="false">
	<cfreturn variables.ConfigPath />
</cffunction>

<cffunction name="setConfigPath" access="private" returntype="void" output="false">
	<cfargument name="configXmlFile" type="string" required="true">
	<cfargument name="hbmXmlDir" type="string" required="true">
	<cfargument name="javafilesdir" type="string" required="true">
	<cfargument name="classfilesdir" type="string" required="true">
	<cfargument name="reverseEngDir" type="string" required="true">
	<cfargument name="hbmExportDir" type="string" required="true">
	<cfset var arg = ""/>
	<cfloop list="#structKeyList(arguments)#" index="arg">
		<cfset variables[arg] = expandPath(arguments[arg]) />
	</cfloop>
</cffunction>

</cfcomponent>